package com.shuai.service.impl;


import com.shuai.constant.MessageConstant;
import com.shuai.dto.PasswordEditDTO;
import com.shuai.dto.EnterpriseDTO;
import com.shuai.dto.EnterpriseLoginDTO;
import com.shuai.entity.Enterprise;
import com.shuai.exception.AccountNotFoundException;
import com.shuai.exception.PasswordErrorException;
import com.shuai.mapper.EnterpriseMapper;
import com.shuai.service.EnterpriseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;



@Service
@Slf4j
public class EnterpriseServiceImpl implements EnterpriseService {

    @Autowired
    private EnterpriseMapper enterpriseMapper;

    /**
     * 企业登录
     *
     * @param enterpriseLoginDTO
     * @return
     */
    public Enterprise login(EnterpriseLoginDTO enterpriseLoginDTO) {
        String enterprisename = enterpriseLoginDTO.getEnterpriseName();
        String password = enterpriseLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Enterprise enterprise = enterpriseMapper.getByEnterpriseName(enterprisename);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (enterprise == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        //对密码进行md5加密改造
        password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));

        if (!password.equals(enterprise.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        //3、返回实体对象
        return enterprise;
    }

    /**
     * 新增企业
     * @param enterpriseDTO
     */
    @Override
    public void save(EnterpriseDTO enterpriseDTO) {
        Enterprise enterprise = new Enterprise();

        //对象属性拷贝
        BeanUtils.copyProperties(enterpriseDTO,enterprise);

        //密码加密
        enterprise.setPassword(DigestUtils.md5DigestAsHex(enterprise.getPassword().getBytes(StandardCharsets.UTF_8)));

        //设置

        //调用持久层插入企业数据
        enterpriseMapper.insert(enterprise);
    }



    /**
     *根据id查询企业信息
     * */
    @Override
    public Enterprise getById(Long id) {
        Enterprise enterprise = enterpriseMapper.getById(id);
        enterprise.setPassword("****");
        return enterprise;
    }

    /**
     * 修改企业信息
     * @param enterpriseDTO
     * */
    @Override
    public void update(EnterpriseDTO enterpriseDTO) {

        //将DTO转换为实体对象,属性拷贝
        Enterprise enterprise = new Enterprise();
        BeanUtils.copyProperties(enterpriseDTO,enterprise);

        //enterprise.setUpdateTime(LocalDateTime.now());
        //enterprise.setUpdateEnterprise(BaseContext.getCurrentId());

        enterpriseMapper.update(enterprise);
    }

    /**
     * 修改密码
     * */
    @Override
    public void updatePassword(PasswordEditDTO passwordEditDTO) {

        //检查旧密码
        int result = enterpriseMapper.checkPassword(passwordEditDTO.getId(), DigestUtils.md5DigestAsHex(passwordEditDTO.getOldPassword().getBytes(StandardCharsets.UTF_8)));
        if (result == 0) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        //更新密码
        enterpriseMapper.updatePassword(passwordEditDTO.getId(), DigestUtils.md5DigestAsHex(passwordEditDTO.getNewPassword().getBytes(StandardCharsets.UTF_8)));
    }
    
}
